﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Main.Master" AutoEventWireup="true" CodeBehind="AnagraficaArticoli.aspx.cs" Inherits="FimetContoDeposito.Anagrafiche.AnagraficaArticoli" %>

<%@ Register assembly="GrayParrot.Web.Controls" namespace="GrayParrot.Web.Controls" tagprefix="cc1" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
    <asp:PlaceHolder ID="PlaceHolder1" runat="server">        
        <%: Styles.Render("~/bundles/Datatables/css") %>
        <%: Scripts.Render("~/bundles/Datatables") %>
        <%: Styles.Render("~/bundles/Formly/css") %>
        <%: Scripts.Render("~/bundles/Formly") %>
    </asp:PlaceHolder>

    <script type="text/javascript" lang="javascript" src="../Scripts/Formly/FormlyHelper.js"></script>
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Core.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.Fileupload.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.Directives.DateTimePicker.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.AngularJS.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.DataTables.js"></script> 
    <script type="text/javascript" lang="javascript" src="../Scripts/GrayParrot.Modals.js"></script> 

<%--    <style>
        td { font-size: 11px; padding: 0px; margin:0px; }
        table thead > tr > th, table tbody > tr > th, table tfoot > tr > th, table thead > tr > td, table tbody > tr > td, table tfoot > tr > td {
          border-top: 1px solid #EDEDED;
          padding: 1px 1px;
        }
    </style>--%>
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <!-- 
        Sitemap Menu item:
        <siteMapNode url="~/AnagraficaFamiglie.aspx" title="AnagraficaFamiglie" description="AnagraficaFamiglie" image="fa-laptop" id="AnagraficaFamiglie"/>
    -->
    <section class="panel">
<%--	    <header class="panel-heading">
			<b>Anagrafica Articoli</b>
		</header>--%>

        <a class="collapsed" data-toggle="collapse" href="#collapseSetting">
            <h4 class="fa  fa-search"> Filters</h4>
        </a>
        <section id="collapseSetting" class="collapse" style="height: 0px;">
            <div class="collapse-boby" style="padding:0">
                <% HttpContext.Current.Response.Write(this.JQDTable.RenderExternalFilters()); %>
            </div>
        </section>

		<div class="panel-body">

            <!-- PUT YOUR CONTENT HERE -->
                <iframe id='iframeCrud' frameborder="0" style="width:1000px; height:550px; display:none"></iframe>
                <div id="crudDialog" title="" style="display:none"></div>
                <div id="deleteDiv" title="" style="display:none"></div>

                <div class="row">
                    <div class="col-lg-12">
                        <section class="panel">
							<header class="panel-heading">
                            <div ng-app="formlyExample" ng-controller="MainCtrl as vm" name="divContainer" id="div1">
                                <div ng-if="vm.loadingData.$$state.status === 0" style="margin:20px 0;font-size:2em">
                                    <strong>Caricamento...</strong>
                                </div>
                                <div ng-if="vm.loadingData.$$status.state !== 0">
                                    <form ng-submit="vm.onSubmit()" novalidate id="form1">
                                        <formly-form model="vm.model" fields="vm.fields" form="vm.form">
                                        </formly-form>
                                    </form>
                                </div>
                            </div>
							</header>  
                            <div class="panel-body">
                                <cc1:JQDataTable ID="JQDTable" runat="server" 
                                     SelectionMode="Single" 
                                     RenderScripts="false" 
                                     IsResponsive="true"
                                     BootstrapClasses="table table-hover table-bordered table-condensed">
                                    <FeaturesConfig>
                                        <Features>
                                            <cc1:Searching Value="Both" ExternalFilters="true">
                                                <Filters>
                                                    <cc1:ColumnFilter ColumnName="Codice" Type="Text" />
                                                    <cc1:ColumnFilter ColumnName="Descrizione" Type="Text" />
                                                    <cc1:CustomChoiceFilter ColumnName="IsXxxx" DefaultLabel="Codice XXXX" FilterDefaultLabel="seleziona...">
                                                         <Data>
                                                            <cc1:DataItem name="Tutti" value="0" />
                                                            <cc1:DataItem name="Si" value="1" />
                                                            <cc1:DataItem name="No" value="2" />
                                                        </Data>
                                                    </cc1:CustomChoiceFilter>
                                                </Filters>
                                            </cc1:Searching>
                                            <cc1:AutoWitdh Value="true" />
                                            <cc1:ScrollY Value="500" ScrollCollapse="true" />
                                            <cc1:LengthChange Value="false" />
                                            <cc1:Paging PagingType="FullNumbers" PageLength="20" />
                                        </Features>
                                    </FeaturesConfig>
                                    <ControllerConfig Url="/api/AnagraficaArticoli/LoadArticolimagazzino" LoadingMode="OnDemand">
                                        <Validation Message="Indicare un Magazzino" FormId="form1" />
                                        <ControllerParameters>
                                            <cc1:ControllerParameter Name="formly_1_asyncselect_selMagazzini_0" Alias="Customer" />
                                        </ControllerParameters>
                                    </ControllerConfig>
                                    <ColumnsConfig Key="Id">
                                        <Columns>
                                            <cc1:TextColumn Name="Id" Header="Id" Visible="false" />
                                            <cc1:TextColumn Name="Codice" Header="Codice" Width="50" />
                                            <cc1:TextColumn Name="DescrCodiceFornitore" Header="Cod.Forn." Width="30" />
                                            <cc1:TextColumn Name="SerieFamiglia" Header="S./Fam." Width="50" />
                                            <cc1:TextColumn Name="DescrTipologia" Header="Tipo" Width="60"/>
                                            <cc1:TextColumn Name="Descrizione" Header="Descrizione" Width="350" />
                                            <cc1:TextColumn Name="Dimensioni" Header="Dim." Width="50" />
                                            <cc1:TextColumn Name="QtaPacco" Header="Qta/P." Width="50" />
                                            <cc1:TextColumn Name="Note" Header="Note" Width="350" />
                                            <cc1:TextColumn Name="DescrUnitaDiMisura" Header="U.M. Grz." Width="60" />
                                            <cc1:TextColumn Name="DescrUMFatt" Header="U.M. Fatt." Width="60" />        
                                            <cc1:TextColumn Name="ScortaMinima" Header="S.Min." Width="60" />                                 
                                            <cc1:TextColumn Name="PesoTeorico" Header="P.T." Width="60"  />
                                            <cc1:TextColumn Name="PrezzoUnitario" Header="P.U." Width="60" />
                                            <cc1:TextColumn Name="ScontoFornitore" Header="Sconto" Width="60" />
                                            <cc1:BooleanColumn Name="IsAbilitato" Header="Abil." Width="30" />
                                            <cc1:BooleanColumn Name="IsXxxx" Header="XXXX" Width="30" />
                                            <cc1:TextColumn Name="IdCliente" Header="IdCliente" Visible="false" />
                                            <cc1:ActionColumn Header="Azioni" >
                                                <Actions>
                                                    <cc1:ActionIcon Icon="fa fa-search" Callback="ShowImage" />
                                                    <cc1:ActionIcon Icon="fa fa-pencil" Callback="EditCallback" />
                                                    <cc1:ActionIcon Icon="fa fa-trash-o" Callback="DeleteCallback" />
                                                </Actions>
                                            </cc1:ActionColumn>
                                        </Columns>
                                    </ColumnsConfig>
                                    <Buttons>
                                        <cc1:CrudAddButton Text="Aggiungi" Callback="LoadAddDialog" />
                                        <cc1:CustomButton Text="Stampa" Callback="Stampa"  />
                                        <cc1:CustomButton Text="Sposta" Callback="Sposta"  />
<%--                                        <cc1:CrudEditButton Text="Modifica" Callback="LoadEditDialog" />
                                        <cc1:CrudDeleteButton Text="Cancella" Callback="DeleteDialog" />--%>
                                    </Buttons>
                                </cc1:JQDataTable>
                            </div>                      
                        </section>
                    </div>
                </div>
		</div>
	</section>

</asp:Content>

<asp:Content ID="Content3" ContentPlaceHolderID="LocaJavascript" runat="server">
    <% HttpContext.Current.Response.Write(this.JQDTable.RenderScriptsManually()); %>

<script>
    $(document).ready(function () {
        // BOOTSTRAP: Tweaks
        $('.yadcf-filter-wrapper').addClass('input-group');
        $('.yadcf-filter, .yadcf-filter-date', this).addClass('form-control input-sm');
        $('.yadcf-filter-reset-button', this).addClass('btn btn-default btn-sm').wrap('<span class="input-group-btn"></span>');
    });

    var settings = new FormlySettings();
    settings.container = '#divFormly';
    settings.setLoad(settings.createGetConfig('/api/AnagraficaArticoli/LoadCustomersFields'));
    FormlyManager(settings);

    function ShowImage(obj) {
        //alert(obj.FotoArticolo);

        $("#iframeCrud").prop('src', '/operazioni/ShowImage.aspx?id=' + obj.Id).appendTo("#crudDialog");
        $("#iframeCrud").width(700);
        $("#iframeCrud").height(550);
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Dettaglio Articolo',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                $("#iframeCrud").width(1000);
                $("#iframeCrud").height(550);
                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function Sposta() {
        var m_idArticolo = GetSelectedData(m_tableJQDTable).Id;

        $("#iframeCrud").prop('src', '/anagrafiche/AnagraficaArticoli_Sposta_Articolo.aspx?id=' + m_idArticolo).appendTo("#crudDialog");
        $("#iframeCrud").width(500);
        $("#iframeCrud").height(300);
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Dettaglio Articolo',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                $("#iframeCrud").width(1000);
                $("#iframeCrud").height(550);
                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function Stampa() {
        var idMagazzino = $('#formly_1_asyncselect_selMagazzini_0').val();
        if (idMagazzino === undefined)
        {
            alert('Seleziona un magazzino...');
            return;
        }

        var url = '/OperationReports/ReportAnagraficaArticoli.aspx?id=' + idMagazzino.replace('string:', '');

        $("#iframeCrud").prop('src', url).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Anagrafica Articoli',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function (response) {
                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function CloseDialogAndReload(response) {
        RedrawGrid(m_tableJQDTable);

        $('#crudDialog').dialog('close');

        alert(response);
    }

    function CloseDialog(response) {
        $('#crudDialog').dialog('close');

        alert(response);
    }

    function LoadAddDialog() {

        var m_id_magazzino = $('#formly_1_asyncselect_selMagazzini_0').val();

        if ((m_id_magazzino === undefined) || (m_id_magazzino == "")) {
            alert('Indicare un Magazzino');
            return;
        }

        m_id_magazzino = m_id_magazzino.replace('string:', '');

        $("#iframeCrud").prop('src', 'AnagraficaArticoli_CRUD.aspx?idMg=' + m_id_magazzino).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Articolo',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function (response) {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
                $('#crudDialog').dialog('close');
            }
        });
    }

    function EditCallback(obj) {
        var p = null;
        p = obj.Id;

        var m_id_magazzino = $('#formly_1_asyncselect_selMagazzini_0').val();
        m_id_magazzino = m_id_magazzino.replace('string:', '');

        var url = 'AnagraficaArticoli_CRUD.aspx?id=' + p + '&idMg=' + m_id_magazzino;

        $("#iframeCrud").prop('src', url).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Articolo',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function (response) {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function DeleteCallback(obj) {
        var p = null;
        var codice = null;
        p = obj.Id;
        codice = obj.Codice;

        var url = '/api/AnagraficaArticoli/Delete';

        $("#deleteDiv").contents().remove();
        $("#deleteDiv").html("Confermi la cancellazione dell'elemento con codice: '" + codice + "' ?").appendTo("#crudDialog");
        //$("#crudDialog").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?");
        $("#deleteDiv").show();

        // Define the Dialog and its properties.
        $("#crudDialog").dialog({
            resizable: false,
            modal: true,
            title: "Cancella elemento",
            height: 250,
            width: 400,
            buttons:[
            {
                id: "Yes",
                text: "Si",
                click: function () {
                    $(this).dialog("option", { buttons: {} });
                    //$(this).empty();
                    $(this).dialog('close');

                    $.ajax({
                        type: "POST",
                        url: url,
                        data: JSON.stringify({ id: p }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            RedrawGrid(m_tableJQDTable);

                            //$("#crudDialog").html('');
                            $("#deleteDiv").contents().remove();
                            $('#deleteDiv').hide();

                            alert(response);
                        },
                        failure: function (response) {

                            //$("#crudDialog").empty();
                            $("#deleteDiv").contents().remove();
                            $('#deleteDiv').hide();

                            alert(response);
                        }
                    });
                }
            },
            {
                id: "No",
                text: "No",
                click: function () {
                    //$("#crudDialog").empty();
                    $("#deleteDiv").contents().remove();
                    $('#deleteDiv').hide();

                    $(this).dialog('close');
                }
            }
            ]
        });
    }

    function LoadEditDialog() {

        var p = null;
        p = GetSelectedData(m_tableJQDTable).Id;

        var m_id_magazzino = $('#formly_1_asyncselect_selMagazzini_0').val();
        m_id_magazzino = m_id_magazzino.replace('string:', '');
               
        var url = 'AnagraficaArticoli_CRUD.aspx?id=' + p + '&idMg=' + m_id_magazzino;

        $("#iframeCrud").prop('src', url).appendTo("#crudDialog");
        $("#iframeCrud").show();

        m_dialog = $("#crudDialog").dialog({
            title: 'Anagrafica Articoli',
            width: 'auto',
            height: 'auto',
            modal: true,
            close: function () {
                RedrawGrid(m_tableJQDTable);

                $("#iframeCrud").contents().remove();
                $('#iframeCrud').hide();
            }
        });
    }

    function DeleteDialog() {
        var p = null;

        var row = GetSelectedData(m_tableJQDTable);

        p = row.Id;

        var url = '/api/AnagraficaArticoli/Delete';

        $("#deleteDiv").contents().remove();
        $("#deleteDiv").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?").appendTo("#crudDialog");
        //$("#crudDialog").html("Confermi la cancellazione dell'elemento con codice: " + p + " ?");
        $("#deleteDiv").show();

        // Define the Dialog and its properties.
        $("#crudDialog").dialog({
            resizable: false,
            modal: true,
            title: "Cancella elemento",
            height: 250,
            width: 400,
            buttons:[
            {
                id: "Yes",
                text: "Si",
                click: function () {
                    $(this).dialog("option", { buttons: {} });
                    //$(this).empty();
                    $(this).dialog('close');

                    $.ajax({
                        type: "POST",
                        url: url,
                        data: JSON.stringify({ id: p }),
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {
                            RedrawGrid(m_tableJQDTable);

                            //$("#crudDialog").html('');
                            $("#deleteDiv").contents().remove();
                            $('#deleteDiv').hide();

                            alert(response);
                        },
                        failure: function (response) {

                            //$("#crudDialog").empty();
                            $("#deleteDiv").contents().remove();
                            $('#deleteDiv').hide();

                            alert(response);
                        }
                    });
                }
            },
            {
                id: "No",
                text: "No",
                click: function () {
                    //$("#crudDialog").empty();
                    $("#deleteDiv").contents().remove();
                    $('#deleteDiv').hide();

                    $(this).dialog('close');
                }
            }
            ]
        });
    }
</script>
</asp:Content>